<template>
  <div class="admin-layout">
    <!-- 侧边菜单栏 -->
    <Sidebar :isCollapse="isCollapse" />

    <!-- 右侧内容区域 -->
    <div class="content-wrapper">
      <!-- 顶部导航栏 -->
      <Navbar
        :isCollapse="isCollapse"
        @toggle-sidebar="toggleSidebar"
        :user-avatar="userAvatar"
      />

      <!-- 标签页 -->
      <div class="tabs-container">
        <el-tabs 
          v-model="activeTab" 
          type="card" 
          @tab-remove="removeTab"
          @tab-click="handleTabClick"
          class="page-tabs"
        >
          <el-tab-pane
            v-for="tab in tabs"
            :key="tab.name"
            :label="tab.title"
            :name="tab.name"
            :closable="tab.closable"
          />
        </el-tabs>
      </div>

      <!-- 主内容区域 -->
      <div class="main-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import Sidebar from '@/components/common/SideBar.vue'
import Navbar from '@/components/common/NavBar.vue'


const router = useRouter()
const route = useRoute()

// 响应式数据
const isCollapse = ref(false) // 控制侧边栏的展开和折叠
const userAvatar = ref('') // 用户头像地址
const activeTab = ref('dashboard') // 当前激活的标签页
const tabs = ref([
  { name: 'dashboard', title: '工作台', closable: false } // 默认标签页
])

// 路由名称映射
const routeNames = {
  '/dashboard': '工作台',
  '/documentList': '文档管理',
  '/knowledgeBase': '知识库管理',
  '/sessionLog': '对话记录',
  '/sessionStats': '对话统计分析',
  '/userView': '个人中心'
}

// 方法
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

const addTab = (path) => {
  const tabName = path === '/dashboard' ? 'dashboard' : path.replace(/\//g, '_')
  if (tabName === 'dashboard') {
    activeTab.value = 'dashboard'
    router.push('/dashboard')
    return
  }

  const title = routeNames[path]
  if (!title) return

  const existingTab = tabs.value.find(tab => tab.name === tabName)
  if (!existingTab) {
    tabs.value.push({
      name: tabName,
      title: title,
      closable: true
    })
  }
  activeTab.value = tabName
}

const removeTab = (tabName) => {
  const index = tabs.value.findIndex(tab => tab.name === tabName)
  if (index > -1) {
    tabs.value.splice(index, 1)
    if (activeTab.value === tabName) {
      const newActiveTab = tabs.value[Math.max(0, index - 1)]
      activeTab.value = newActiveTab.name
      if (newActiveTab.name === 'dashboard') {
        router.push('/dashboard')
        return
      }
      const path = newActiveTab.name.replace(/_/g, '/')
      router.push(path)
    }
  }
}

const handleTabClick = (tab) => {
  const path = tab.props.name.replace(/_/g, '/')
  if (path === 'dashboard') {
    activeTab.value = 'dashboard'
    router.push('/dashboard')
    return
  }
  router.push(path)
}

// 监听路由变化
watch(() => route.path, (newPath) => {
  addTab(newPath)
}, { immediate: true })

</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.content-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  min-width: 0;
  width: 100%;
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95) 0%, rgba(22, 33, 62, 0.95) 100%);
  border-radius: 16px 0 0 16px;
  /* margin: 8px 8px 8px 0; */
  box-shadow: 0 8px 32px rgba(0, 212, 255, 0.1);
  border: 1px solid rgba(0, 212, 255, 0.2);
  backdrop-filter: blur(20px);
}

.tabs-container {
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95) 0%, rgba(22, 33, 62, 0.95) 100%);
  border-bottom: 1px solid rgba(0, 212, 255, 0.2);
  padding: 0 24px;
  height: 52px;
  flex-shrink: 0;
  width: 100%;
  box-shadow: 0 4px 20px rgba(0, 212, 255, 0.1);
  border-radius: 16px 16px 0 0;
  backdrop-filter: blur(20px);
}

.page-tabs {
  margin-top: 0;
  width: 100%;
}

.page-tabs :deep(.el-tabs__header) {
  margin-bottom: 0;
  width: 100%;
  border: none;
}

.page-tabs :deep(.el-tabs__nav) {
  border: none;
  width: 100%;
}

.page-tabs :deep(.el-tabs__item) {
  border: 1px solid rgba(0, 212, 255, 0.3);
  border-bottom: none;
  margin-right: 8px;
  border-radius: 12px 12px 0 0;
  height: 52px;
  line-height: 52px;
  padding: 0 24px;
  font-size: 15px;
  font-weight: 600;
  color: #ffffff;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.8) 0%, rgba(22, 33, 62, 0.8) 100%);
  backdrop-filter: blur(10px);
}

.page-tabs :deep(.el-tabs__item:hover) {
  color: #00d4ff;
  border-color: #00d4ff;
  background: linear-gradient(135deg, rgba(0, 212, 255, 0.1) 0%, rgba(0, 153, 204, 0.1) 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 212, 255, 0.2);
}

.page-tabs :deep(.el-tabs__item.is-active) {
  background: linear-gradient(135deg, #00d4ff 0%, #0099cc 100%);
  color: #ffffff;
  border-color: #00d4ff;
  border-bottom-color: transparent;
  box-shadow: 0 4px 12px rgba(0, 212, 255, 0.3);
  position: relative;
}

.page-tabs :deep(.el-tabs__item.is-active)::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #00d4ff 0%, #0099cc 100%);
  border-radius: 3px 3px 0 0;
}

.page-tabs :deep(.el-tabs__nav-prev),
.page-tabs :deep(.el-tabs__nav-next) {
  height: 44px;
  line-height: 44px;
  color: rgba(255, 255, 255, 0.7);
}

.page-tabs :deep(.el-tabs__nav-prev:hover),
.page-tabs :deep(.el-tabs__nav-next:hover) {
  color: #00d4ff;
}

.main-content {
  background: linear-gradient(135deg, rgba(15, 15, 35, 0.9) 0%, rgba(26, 26, 46, 0.9) 50%, rgba(22, 33, 62, 0.9) 100%);
  padding: 32px;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-radius: 0 0 16px 16px;
  position: relative;
}

/* 添加科技感装饰元素 */
.main-content::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent 0%, #00d4ff 50%, transparent 100%);
  opacity: 0.5;
}

.main-content::after {
  content: '';
  position: absolute;
  top: 20px;
  right: 20px;
  width: 100px;
  height: 100px;
  background: radial-gradient(circle, rgba(0, 212, 255, 0.1) 0%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
}

/* 自定义滚动条 */
.main-content::-webkit-scrollbar {
  width: 8px;
}

.main-content::-webkit-scrollbar-track {
  background: rgba(26, 26, 46, 0.8);
  border-radius: 4px;
}

.main-content::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #00d4ff 0%, #0099cc 100%);
  border-radius: 4px;
  transition: all 0.3s ease;
}

.main-content::-webkit-scrollbar-thumb:hover {
  background: linear-gradient(180deg, #0099cc 0%, #00d4ff 100%);
}

/* 全局下拉菜单层级 */
:global(.el-dropdown-menu),
:global(.el-popper) {
  z-index: 9999 !important;
  box-shadow: 0 20px 40px rgba(0, 212, 255, 0.2);
  border-radius: 16px;
  border: 1px solid rgba(0, 212, 255, 0.3);
  backdrop-filter: blur(20px);
  background: linear-gradient(135deg, rgba(26, 26, 46, 0.95) 0%, rgba(22, 33, 62, 0.95) 100%) !important;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .main-content {
    padding: 24px;
  }
}

@media (max-width: 768px) {
  .content-wrapper {
    margin: 4px 4px 4px 0;
    border-radius: 12px 0 0 12px;
    border: 1px solid rgba(0, 212, 255, 0.2);
  }
  
  .tabs-container {
    padding: 0 16px;
    height: 48px;
  }
  
  .main-content {
    padding: 20px;
  }
  
  .page-tabs :deep(.el-tabs__item) {
    padding: 0 16px;
    font-size: 14px;
    height: 48px;
    line-height: 48px;
  }
}

@media (max-width: 480px) {
  .content-wrapper {
    margin: 2px 2px 2px 0;
    border-radius: 8px 0 0 8px;
    border: 1px solid rgba(0, 212, 255, 0.2);
  }
  
  .tabs-container {
    padding: 0 12px;
    height: 44px;
  }
  
  .main-content {
    padding: 16px;
  }
  
  .page-tabs :deep(.el-tabs__item) {
    padding: 0 12px;
    margin-right: 4px;
    font-size: 13px;
    height: 44px;
    line-height: 44px;
  }
}
</style>